﻿using System;
using System.ComponentModel;

using DevExpress.Xpo;

using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp;


namespace ECRM.Module
{
    [Custom("Caption", "公共信息")]
    [Custom("IsVisibleInReports", "True")]
    public abstract class CommonInfo : BaseObject
    {
        public CommonInfo(Session session)
            : base(session)
        {

        }
        private User _creator;
        [Custom("AllowEdit", "False")]
        [Custom("Caption", "创建者")]
        public User Creator
        {
            get
            {
                if (_creator == null)
                {
                    _creator = Session.GetLoadedObjectByKey<User>(SecuritySystem.CurrentUserId);
                }
                return _creator;
            }
            set
            {
                SetPropertyValue<User>("Creator", ref _creator, value);
            }
        }
        private DateTime _createTime;
        [Custom("Caption", "创建时间")]
        [Custom("AllowEdit", "False")]
        public DateTime CreateTime
        {
            get
            {
                if (_createTime == DateTime.MinValue)
                {
                    _createTime = DateTime.Now;
                }
                return _createTime;
            }
            set
            {
                SetPropertyValue<DateTime>("CreateTime", ref _createTime, value);
            }
        }
        private DateTime _lastUpdateTime;
        [Custom("Caption", "最后更新时间")]
        [Custom("AllowEdit", "False")]
        public DateTime LastUpdateTime
        {
            get
            {
                if (_lastUpdateTime == DateTime.MinValue)
                {
                    _lastUpdateTime = DateTime.Now;
                }
                return _lastUpdateTime;
            }
            set
            {
                SetPropertyValue<DateTime>("LastUpdateTime", ref _lastUpdateTime, DateTime.Now);
            }
        }
        private string _remark;
        [Custom("Caption", "备注")]
        [Size(1000)]
        public string Remark
        {
            get
            {
                return _remark;
            }
            set
            {
                SetPropertyValue<string>("Remark", ref _remark, value);
            }
        }

        private XPCollection<AuditDataItemPersistent> auditTrail;
        [Custom("Caption", "数据修改记录")]
        public XPCollection<AuditDataItemPersistent> AuditTrail
        {
            get
            {
                if (auditTrail == null)
                {
                    auditTrail = AuditedObjectWeakReference.GetAuditTrail(Session, this);
                }
                return auditTrail;
            }

        }
    }

    //[Custom("Caption","分类")]
    //[DefaultProperty("Name")]
    //public abstract class CommonCategory : CommonInfo, IHCategory
    //{
    //    public CommonCategory(Session session)
    //        : base(session)
    //    {
    //    }
    //    public CommonCategory(Session session, string name)
    //        : this(session)
    //    {
    //        _name = name;
    //    }

    //    private string _name;
    //    [Custom("Caption", "名称")]
    //    public string Name
    //    {
    //        get
    //        {
    //            return _name;
    //        }
    //        set
    //        {
    //            SetPropertyValue<string>("Name", ref _name, value);
    //        }
    //    }
    //    private CommonCategory _parent;
    //    [Association("CommonCategoryParent-CommonCategoryChild")]
    //    [Custom("Caption", "父节点")]
    //    public CommonCategory Parent
    //    {
    //        get
    //        {
    //            return _parent;
    //        }
    //        set
    //        {
    //            SetPropertyValue<CommonCategory>("Parent", ref _parent, value);
    //        }
    //    }
    //    // Properties
    //    [Association("CommonCategoryParent-CommonCategoryChild")]
    //    [Custom("Caption", "子分类")]
    //    public XPCollection<CommonCategory> Children
    //    {
    //        get
    //        {
    //            return base.GetCollection<CommonCategory>("Children");
    //        }
    //    }
    //    private int _sortIndex;
    //    [Custom("Caption", "排序")]
    //    public int SortIndex
    //    {
    //        get { return _sortIndex; }
    //        set { SetPropertyValue<int>("SortIndex", ref _sortIndex, value); }
    //    }

    //    ITreeNode IHCategory.Parent
    //    {
    //        get
    //        {
    //            return Parent;
    //        }
    //        set
    //        {
    //            Parent = value as CommonCategory;
    //        }
    //    }

    //    IBindingList ITreeNode.Children
    //    {
    //        get
    //        {
    //            return Children;
    //        }
    //    }

    //    ITreeNode ITreeNode.Parent
    //    {
    //        get
    //        {
    //            return Parent;
    //        }
    //    }

    //    [NonPersistent, RuleFromBoolProperty("CommonCategoryCircularReferences", DefaultContexts.Save, "Circular refrerence detected. To correct this error, set the Parent property to another value.", UsedProperties = "Parent"), Browsable(false)]
    //    public bool IsValid
    //    {
    //        get
    //        {
    //            for (CommonCategory category = this.Parent; category != null; category = category.Parent)
    //            {
    //                if (category == this)
    //                {
    //                    return false;
    //                }
    //            }
    //            return true;
    //        }
    //    }
    //}

    [Custom("Caption", "单据")]
    [DefaultProperty("OrderNO")]
    public abstract class AbsOrder : CommonInfo
    {
        public AbsOrder(Session session)
            : base(session)
        {

        }
        protected override void OnSaving()
        {
            base.OnSaving();

            if (Session.IsNewObject(this))
            {
                if (OrderNO == ConstString.EmptyOrderNO)
                {
                    OrderNO = SeedNum.GetSeedNum(Session, SeedType.ToString());
                }
            }
        }

        protected abstract SeedType SeedType { get; }
        private string _orderNO = ConstString.EmptyOrderNO;
        [Custom("Caption", "单据编号")]
        [Custom("AllowEdit", "False")]
        public string OrderNO
        {
            get { return _orderNO; }
            set { SetPropertyValue<string>("OrderNO", ref _orderNO, value); }
        }
        private Customer _customer;
        [Custom("Caption", "客户")]
        public Customer Customer
        {
            get { return _customer; }
            set
            {
                SetPropertyValue<Customer>("Customer", ref _customer, value);
            }
        }

        private Currency _currency;
        [Custom("Caption", "币别")]
        public Currency Currency
        {
            get { return _currency; }
            set
            {
                SetPropertyValue<Currency>("Currency", ref _currency, value);
            }
        }
        private bool _isConfirm;
        [Custom("Caption", "是否已经审批")]
        public bool IsConfirm
        {
            get { return _isConfirm; }
            set
            {
                SetPropertyValue<bool>("IsConfirm", ref _isConfirm, value);
            }
        }

        private User _confirmUser;
        [Custom("Caption", "审批人")]
        public User ConfirmUser
        {
            get { return _confirmUser; }
            set
            {
                SetPropertyValue<User>("ConfirmUser", ref _confirmUser, value);
            }
        }
        private string _condition;
        [Custom("Caption", "条件与条款")]
        [Size(1000)]
        public string Condtion
        {
            get { return _condition; }
            set
            {
                SetPropertyValue<string>("Condition", ref _condition, value);
            }
        }
        [Custom("Caption", "单据中的产品详细信息")]
        [Association("AbsOrder-ProductDetails")]
        public XPCollection<OrderProductDetail> OrderProductDetails
        {
            get
            {
                return GetCollection<OrderProductDetail>("OrderProductDetails");
            }
        }
    }
}
